package com.neo.util;

import com.neo.fastdfs.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Component
public class FastDFSUtil {

	private static Logger logger = LoggerFactory.getLogger(FastDFSUtil.class);

	/**
	 * 上传文件
	 * @param file
	 * @return
     */
	public static String[] upload(FastDFSFile file) {
		logger.info("File Name: " + file.getName() + "File Length:" + file.getContent().length);
		NameValuePair[] meta_list = new NameValuePair[1];
		meta_list[0] = new NameValuePair("author", file.getAuthor());
		long startTime = System.currentTimeMillis();
		String[] uploadResults = null;
		TrackerServer trackerServer = TrackerServerPool.borrowObject();
		StorageClient1 storageClient = new StorageClient1(trackerServer, null);
		try {
			uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
			TrackerServerPool.returnObject(trackerServer);
		} catch (IOException e) {
			logger.error("IO Exception when uploadind the file:" + file.getName(), e);
		} catch (Exception e) {
			logger.error("Non IO Exception when uploadind the file:" + file.getName(), e);
		}
		logger.info("upload_file time used:" + (System.currentTimeMillis() - startTime) + " ms");
		if (uploadResults == null && storageClient!=null) {
			logger.error("upload file fail, error code:" + storageClient.getErrorCode());
		}
		String groupName = uploadResults[0];
		String remoteFileName = uploadResults[1];
		logger.info("upload file successfully!!!" + "group_name:" + groupName + ", remoteFileName:" + " " + remoteFileName);
		return uploadResults;
	}

	/**
	 * @param groupName
	 * @param remoteFileName
     * @return
     */
	public static FileInfo getFile(String groupName, String remoteFileName) {
		try {
			TrackerServer trackerServer = TrackerServerPool.borrowObject();
			StorageClient1 storageClient = new StorageClient1(trackerServer, null);
			FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFileName);
			TrackerServerPool.returnObject(trackerServer);
			return fileInfo;
		} catch (IOException e) {
			logger.error("IO Exception: Get File from Fast DFS failed", e);
		} catch (Exception e) {
			logger.error("Non IO Exception: Get File from Fast DFS failed", e);
		}
		return null;
	}

	/**
	 * @param groupName
	 * @param remoteFileName
     * @return
     */
	public static InputStream downFile(String groupName, String remoteFileName) {
		try {
			TrackerServer trackerServer = TrackerServerPool.borrowObject();
			StorageClient1 storageClient = new StorageClient1(trackerServer, null);
			byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
			TrackerServerPool.returnObject(trackerServer);
			InputStream ins = new ByteArrayInputStream(fileByte);
			return ins;
		} catch (IOException e) {
			logger.error("IO Exception: Get File from Fast DFS failed", e);
		} catch (Exception e) {
			logger.error("Non IO Exception: Get File from Fast DFS failed", e);
		}
		return null;
	}

	/**
	 * @param groupName
	 * @param remoteFileName
	 * @throws Exception
     */
	public static void deleteFile(String groupName, String remoteFileName)throws Exception {
		TrackerServer trackerServer = TrackerServerPool.borrowObject();
		StorageClient1 storageClient = new StorageClient1(trackerServer, null);
		int i = storageClient.delete_file(groupName, remoteFileName);
		TrackerServerPool.returnObject(trackerServer);
		logger.info("delete file successfully!!!" + i);
	}

	public static StorageServer[] getStoreStorages(String groupName)throws IOException {
		TrackerClient trackerClient = new TrackerClient();
		TrackerServer trackerServer = TrackerServerPool.borrowObject();
		StorageServer[] storageServers= trackerClient.getStoreStorages(trackerServer, groupName);
		TrackerServerPool.returnObject(trackerServer);
		return storageServers;
	}

	public static ServerInfo[] getFetchStorages(String groupName,String remoteFileName) throws IOException {
		TrackerClient trackerClient = new TrackerClient();
		TrackerServer trackerServer = TrackerServerPool.borrowObject();
		ServerInfo[] serverInfos= trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
		TrackerServerPool.returnObject(trackerServer);
		return serverInfos;
	}

	public static String getTrackerUrl() throws IOException {
		TrackerServer trackerServer = TrackerServerPool.borrowObject();
		String trackerUrl= "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
		TrackerServerPool.returnObject(trackerServer);
		return trackerUrl;
	}
}